package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.CollectionShopAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetCollectionShop;
import dhm.com.dhmshop.utils.SpUtils;

public class CollectionshopActivity extends BaseActiity implements LoginContract.IView {


    @BindView(R.id.recy)
    RecyclerView recy;


    private PressenterImpl pressenter;
    private String uid;
    private CollectionShopAdapter collectionShopAdapter;
    private GetCollectionShop getCollectionShop;

    @Override
    protected int getLayout() {
        return R.layout.activity_collectionshop;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(CollectionshopActivity.this);
        uid = SpUtils.getString(CollectionshopActivity.this, "uid");
        LinearLayoutManager manager=new LinearLayoutManager(CollectionshopActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        collectionShopAdapter = new CollectionShopAdapter(CollectionshopActivity.this);
        collectionShopAdapter.setdeleteClick(new CollectionShopAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("type","1");
                map.put("cid",getCollectionShop.getData().get(posttion).getFavoriteId()+"");
                pressenter.sendMessage(CollectionshopActivity.this,Constant.DelShopCollect,map, Bean.class);
            }
        });
        recy.setAdapter(collectionShopAdapter);
    }

    @Override
    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        pressenter.sendMessage(CollectionshopActivity.this, Constant.MyShopCollects, map, GetCollectionShop.class);
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetCollectionShop) {
            getCollectionShop = (GetCollectionShop) data;
            if (getCollectionShop.getCode() == 1) {
                collectionShopAdapter.setShopList(getCollectionShop.getData());
            }else {
                collectionShopAdapter.clearShopList();
            }
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(CollectionshopActivity.this,bean.getMessage() , Toast.LENGTH_SHORT).show();
            initData();
        }
    }

    @Override
    public void fail(String error) {

    }
}
